# coding=utf-8
#================================================================
#   Copyright (C) 2020 * Ltd. All rights reserved.
#
#   File name   : MINIO.py
#   Author      : ZouZheKang-1996
#   Created date: 2020-08-14 18:15:08
#   Description : XuQingQing,I really ❤ U！
#
#================================================================

import os
from minio import Minio

#  上传数据，并存放
minioClient = Minio(endpoint='xxx',
                    access_key='xxxxx',
                    secret_key='xxxxx',
                    secure=False)
bucket_name = 'xxx'
object_path = 'zzk/'
path = 'C:/Users/zzk_xqq/Desktop/temp'

if  minioClient.bucket_exists(bucket_name):
    objects  =  os.listdir(path)
    for  obj  in  objects:
        print(obj)
        minio_path  =  os.path.join(object_path,  obj)
        try:
                minioClient.fput_object(bucket_name,  minio_path,  os.path.join(path,  obj))
        except  ResponseError  as  err:
                print(err)
else:
    raise  ValueError("there  is  no  bucket  in:  {}".format(bucket_name))

#  下载数据，并存放
dataset_id_path = 'C:/Users/zzk_xqq/Desktop/download'

if  minioClient.bucket_exists(bucket_name):
    objects  =  minioClient.list_objects(bucket_name,  prefix=object_path)
    for  obj  in  objects:
        break
        if  not  os.path.exists(os.path.join(dataset_id_path,  os.path.basename(obj.object_name))):
            print(obj.object_name)
            try:
                    minioClient.fget_object(bucket_name,  obj.object_name,  os.path.join(
                            dataset_id_path,  os.path.basename(obj.object_name)))
            except  ResponseError  as  err:
                    print(err)
else:
    raise  ValueError("there  is  no  bucket  in:  {}".format(bucket_name))
    
# 删除

object_name = 'zzk\\wait_delete.txt'
minioClient.remove_object(bucket_name, object_name)